package com.mz.ieann.dokit.sdk.interceptor

import okhttp3.CacheControl
import okhttp3.Interceptor
import okhttp3.Response
import java.util.concurrent.TimeUnit

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> 设置缓存策略，超时时间等
 * <p> // 只使用缓存 cacheBuilder.onlyIfCached()
 * <p> // 不使用缓存 cacheBuilder.noCache()
 * <p> // 不使用缓存，不添加缓存 cacheBuilder.noStore()
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2021 2021/11/14 15:15
 */
class HttpInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {

        // 设置缓存配置
        val cacheBuilder = CacheControl.Builder()
        // 禁止转码
        cacheBuilder.noTransform()
        // 指示客户机可以接收生存期不大于指定时间的响应
        cacheBuilder.maxAge(0, TimeUnit.SECONDS)
        // 指示客户机可以接收超出超时期间的响应消息
        cacheBuilder.maxStale(30, TimeUnit.DAYS)
        // 获取到下层的Request
        val request = chain.request()

        return chain.proceed(request)
    }

}